var _api = _interopRequireDefault(require("../../../../utils/api")), _util2 = _interopRequireDefault(require("../util"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _toConsumableArray(t) {
    return _arrayWithoutHoles(t) || _iterableToArray(t) || _nonIterableSpread();
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance");
}

function _iterableToArray(t) {
    if (Symbol.iterator in Object(t) || "[object Arguments]" === Object.prototype.toString.call(t)) return Array.from(t);
}

function _arrayWithoutHoles(t) {
    if (Array.isArray(t)) {
        for (var e = 0, n = new Array(t.length); e < t.length; e++) n[e] = t[e];
        return n;
    }
}

function _objectSpread(e) {
    for (var t = 1; t < arguments.length; t++) {
        var n = null != arguments[t] ? arguments[t] : {}, i = Object.keys(n);
        "function" == typeof Object.getOwnPropertySymbols && (i = i.concat(Object.getOwnPropertySymbols(n).filter(function(t) {
            return Object.getOwnPropertyDescriptor(n, t).enumerable;
        }))), i.forEach(function(t) {
            _defineProperty(e, t, n[t]);
        });
    }
    return e;
}

function _defineProperty(t, e, n) {
    return e in t ? Object.defineProperty(t, e, {
        value: n,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = n, t;
}

var SELECTABLE = {
    type: {
        name: "客户类型"
    },
    status: {
        name: "客户状态"
    },
    labels: {
        name: "自定义标签"
    }
}, colors = function(t) {
    return "<style color: #09bb07;>[".concat(t, "]</style>");
};

Page({
    data: {
        loading: !1
    },
    onLoad: function(t) {
        this.initData(t), this.getLabel(t);
    },
    onUnload: function() {
        this.labelChange && (this.$emit("niche.lists.label.update"), this.labelChange = !1);
    },
    onReady: function() {
        this.custom = this.selectComponent("#custom");
    },
    initData: function(t) {
        var e = t.guid, n = t.cardid, i = t.touid, a = t.type, o = void 0 === a ? -1 : a, r = t.status, l = void 0 === r ? -1 : r, s = t.setting, u = void 0 !== s && s, c = {
            setting: u
        };
        this.query = {
            guid: e,
            cardid: n,
            touid: i
        }, this.form = {}, u || (c.typeSelectid = +o, c.statusSelectid = +l), this.setData(c);
    },
    getLabel: function(t) {
        var n = this, e = t.guid, i = t.setting, a = t.labelids;
        _api.default.minicard.businesslabel.view({
            data: {
                guid: e
            }
        }).then(function(t) {
            var e = t.data;
            n.setData({
                SELECTABLE: n.generateOption(e, a),
                "form.labelids": e.labels.map(function(t) {
                    return t.id;
                }).join(",")
            }), i || n.formerName();
        });
    },
    generateOption: function(e, n) {
        return this.data.setting || (this.form.labelids = n, this.oirginLabelids = n, e.labels.forEach(function(t) {
            t.selected = n.includes(t.id);
        })), Object.keys(SELECTABLE).forEach(function(t) {
            SELECTABLE[t].option = e[t];
        }), SELECTABLE;
    },
    formerName: function() {
        var t = this.data, e = t.typeSelectid, n = t.statusSelectid;
        this.storage = {
            originalType: this.getName(e),
            originalStatus: this.getName(n, 1)
        };
    },
    getName: function(e) {
        var t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : 0, n = SELECTABLE[[ "type", "status", "labels" ][t]].option.find(function(t) {
            return +t.id == +e;
        });
        return n ? n.name : "";
    },
    addLabel: function(t) {
        var i = this, a = t.detail.value;
        _api.default.minicard.businesslabel.create({
            data: _objectSpread({
                name: a
            }, this.query)
        }).then(function(t) {
            var e = t.data;
            i.labelChange = !0;
            var n = SELECTABLE.labels;
            i.custom.hideModal(), n.option.push({
                id: e,
                name: a
            }), i.setData({
                SELECTABLE: SELECTABLE
            }), wx.showToast({
                title: "添加成功"
            });
        });
    },
    deleteLabel: function(t) {
        var e = this, n = t.detail, i = n.id, a = n.index;
        wx.showModal({
            content: "确定删除该标签？",
            success: function(t) {
                t.confirm && _api.default.minicard.businesslabel.delete({
                    data: {
                        labelid: i
                    }
                }).then(function() {
                    SELECTABLE.labels.option.splice(a, 1), e.setData({
                        SELECTABLE: SELECTABLE
                    }), e.labelChange = !0, wx.showToast({
                        title: "删除成功"
                    });
                });
            }
        });
    },
    selectRadio: function(t) {
        var e = t.detail, n = Object.keys(e)[0];
        this.form[n] = +e[n];
    },
    selectMore: function(t) {
        var e = t.detail.option;
        this.query.setting || (SELECTABLE.labels.option = e, this.form.labelids = e.filter(function(t) {
            return t.selected;
        }).map(function(t) {
            return t.id;
        }).join(","), this.setData({
            SELECTABLE: SELECTABLE
        }));
    },
    save: function() {
        var t = this, e = function() {
            wx.successTip({
                title: "保存成功",
                success: function() {
                    return t.$route("back");
                }
            });
        };
        if (this.data.setting) return this.labelChange && (this.$emit("niche.lists.label.update"), 
        this.labelChange = !1), e();
        var n = this.form || {};
        this.checkIsChange().then(function() {
            _api.default.minicard.business.update({
                loading: "保存中...",
                data: _objectSpread({}, n, t.query)
            }).then(e);
        });
    },
    checkIsChange: function() {
        var t = [], e = this.form || {}, n = this.data, i = n.typeSelectid, a = n.statusSelectid, o = this.storage, r = o.originalType, l = o.originalStatus, s = e.type, u = e.status;
        return s && +s != +i && t.push(this.generateFollow(r, s, 0)), u && +u != +a && t.push(this.generateFollow(l, u, 1)), 
        Promise.all(t.concat([ this.checkCustomChange() ]));
    },
    checkCustomChange: function() {
        var t, e, n = [], i = this.oirginLabelids, a = this.form.labelids;
        return (t = a.split(",").filter(function(t) {
            return !i.includes(t);
        })) && t.length && n.push(this.relateLabels(t.join(","), 1)), (e = i.split(",").filter(function(t) {
            return !a.includes(t);
        })) && e.length && n.push(this.relateLabels(e.join(","), 2)), console.log(t, e), 
        t.length || e.length ? Promise.all(n.concat([ this.customLabelsFollow(t, e) ])) : Promise.resolve();
    },
    relateLabels: function(t, e) {
        return _api.default.minicard.businesslabel.relate({
            data: _objectSpread({
                op: e,
                labelid: t
            }, this.query)
        });
    },
    customLabelsFollow: function() {
        var e = this, t = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : [], n = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : [], i = [], a = function(t) {
            return _toConsumableArray(t).map(function(t) {
                return e.getName(t, 2);
            });
        };
        return i = t.length && !n.length ? [ a(t), 0 ] : n.length && !t.length ? [ [], 1, a(n) ] : [ a(t), 2, a(n) ], 
        this.generateCustomFollow.apply(this, _toConsumableArray(i));
    },
    generateCustomFollow: function() {
        var t = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : [], e = 1 < arguments.length ? arguments[1] : void 0, n = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : [], i = function(t) {
            if (1 < t.length) {
                var n = "";
                return t.forEach(function(t, e) {
                    n += "".concat(0 === e ? "" : "，").concat(colors(t));
                }), n;
            }
            return colors(t[0]);
        }, a = this.form.labelids;
        return this.$emit("niche.label.change", {
            type: "labelids",
            oldName: n,
            newName: t,
            id: a
        }, e), this.updateFollow([ "添加".concat(i(t), "标签"), "删除".concat(i(n), "标签"), "自定义标签：".concat(i(n), "更改为").concat(i(t)) ][e]);
    },
    generateFollow: function(t, e, n) {
        var i = [ "type", "status" ], a = this.getName(e, n);
        return this.$emit("niche.label.change", {
            type: i[n],
            oldName: [ t ],
            newName: [ a ],
            id: e
        }, 2), this.updateFollow("".concat(SELECTABLE[i[n]].name, "：").concat(colors(t), "更改为").concat(colors(a)));
    },
    updateFollow: function(n) {
        var i = this;
        return _util2.default.addFollowRecord(_objectSpread({
            content: n
        }, this.query)).then(function(t) {
            var e = t.data;
            return i.$emit("niche.record.update", {
                content: n,
                data: e
            }), Promise.resolve();
        });
    }
});